package com.jfinal.weixin.sdk.jfinal;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.Cookie;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.weixin.sdk.api.SnsAccessTokenApi;
import com.jfinal.weixin.sdk.kit.PaymentKit;

public class OAuthterceptor implements Interceptor {

	public void intercept(Invocation ai) {
		Controller c = ai.getController();
		System.out.println(ai.getActionKey()+","+ai.getMethodName()+","+ai.getViewPath());
		boolean authed = false;
		authed=(c.getSessionAttr("openid")!=null);
		if (!authed) {
			c.setSessionAttr("interceptUrl", c.getRequest().getRequestURL().toString());
			String url;
			try {
				url = SnsAccessTokenApi.getAuthorizeURL(
						PropKit.get("appId"),
							PaymentKit.urlEncode( "http://qg.tunnel.qydev.com/oauth/code")
						, "1", true);
				c.redirect(url);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return;
		} else {
			ai.invoke();
		}

	}

}
